package com.mrx.pvzcommunity.service;

import com.mrx.pvzcommunity.dao.ZombieDao;
import com.mrx.pvzcommunity.model.Response;
import com.mrx.pvzcommunity.model.Result;
import com.mrx.pvzcommunity.model.pvz.Zombie;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ZombieService {

    private final ZombieDao zombieDao;

    private final Base64.Decoder decoder = Base64.getDecoder();

    private static final Logger logger = LoggerFactory.getLogger(ZombieService.class);

    public Result<List<Zombie>> getAllZombies() {
        List<Zombie> res = zombieDao.getAllZombies();
        if (res.isEmpty()) {
            return Result.createResult(Response.Code.NOT_FOUND, Collections.emptyList());
        }
        return Result.createResult(Response.Code.SUCCESS, res);
    }

    public byte[] getImgByZombieId(String pid) {
        String img = zombieDao.getImgByZombieId(pid);
        logger.info("get img: {}", pid);
        try {
            return decoder.decode(img);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return null;
    }
}
